var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    console.log('[master] ' + "start master...");

    for (var i = 0; i < numCPUs; i++) {
         cluster.fork();
    }

    cluster.on('listening', function (worker, address) {
        console.log('[master] ' + 'listening: worker' + worker.id + ',pid:' + worker.process.pid + ', Address:' + address.address + ":" + address.port);
    });

} else if (cluster.isWorker) {
    console.log('[worker] ' + "start worker ..." + cluster.worker.id);
 // 工作进程分支，启动服务器 
    var app = require('./app'); 
//    app.listen(80, function() {
//		console.log('Express server listening on port ' + 80);
//	});
	http.createServer(function (req, res) {
		var util = require('util');
        console.log('worker'+cluster.worker.id+":"+util.inspect(app));
        res.end('worker'+cluster.worker.id+',PID:'+process.pid);
    }).listen(80, function() {
		console.log('Express server listening on port ' + 80);
	});
}